#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 添加4列
    ui->twStudent->setColumnCount(4);

    // 表头
    QStringList rowHraderList;
    rowHraderList.append("姓名");
    rowHraderList.append("性别");
    rowHraderList.append("年龄");
    rowHraderList.append("籍贯");
    ui->twStudent->setHorizontalHeaderLabels(rowHraderList);

    // 设置列的宽度
    ui->twStudent->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    addOneRowData("李雷", "男", 14, "广东");
    addOneRowData("韩梅梅", "女", 13, "广西");
    addOneRowData("林涛", "男", 15, "湖南");

}

Widget::~Widget()
{
    delete ui;
}

/** 添加一行数据
 * @brief Widget::addOneRowData
 * @param name
 * @param gender
 * @param age
 * @param province
 */
void Widget::addOneRowData(QString name, QString gender, int age, QString province)
{
    int count = ui->twStudent->rowCount();

    ui->twStudent->setRowCount(count + 1);

    QTableWidgetItem* nameItem = new QTableWidgetItem(name);
    QTableWidgetItem* genderItem = new QTableWidgetItem(gender);
    QTableWidgetItem* ageItem = new QTableWidgetItem(QString::number(age));
    QTableWidgetItem* provinceItem = new QTableWidgetItem(province);

    nameItem->setTextAlignment(Qt::AlignCenter);
    genderItem->setTextAlignment(Qt::AlignCenter);
    ageItem->setTextAlignment(Qt::AlignCenter);
    provinceItem->setTextAlignment(Qt::AlignCenter);

    ui->twStudent->setItem(count, 0, nameItem);
    ui->twStudent->setItem(count, 1, genderItem);
    ui->twStudent->setItem(count, 2, ageItem);
    ui->twStudent->setItem(count, 3, provinceItem);
}
